package com.naizhuai.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.R;
import com.naizhuai.ui.components.SpinerPopWindow;
import com.naizhuai.ui.components.TipDialog;
import com.naizhuai.ui.components.listener.PopUpWindowMissListener;
import com.naizhuai.ui.components.listener.PopupItemListener;
import com.naizhuai.ui.components.listener.SpinnerListener;
import com.naizhuai.ui.entity.management.CompanyBean;
import com.naizhuai.ui.management.presenter.CompanyManageDataApiStore;
import com.naizhuai.ui.management.presenter.CompanyManageDataApiStoreIml;
import com.naizhuai.ui.presenter.InputUserPhonePresenter;
import com.naizhuai.util.autocomplete.Autocomplete;
import com.naizhuai.util.autocomplete.AutocompleteCallback;
import com.naizhuai.util.autocomplete.AutocompletePresenter;

import java.util.Arrays;
import java.util.List;

import rx.Observer;

/**
 * 添加人员工具类
 * Created by code_han on 2018/3/5.
 */

public class AddPersonUtils {

    public static final int ADD_TYPE_TO_COMPANY = 1;
    public static final int ADD_TYPE_TO_COMPANY_DEPARTMENT = 2;
    public static final int ADD_TYPE_TO_COMPANY_TEAM = 2;

    private Context mContext;

    private PersonUserBean inviteUser;

    private String departmentId;

    private String powerType;


    private int addType;

    private UidUtil uids;

    private AddPersonListener addListener;

    public AddPersonUtils(Context mContext, String departmentId, int addType,AddPersonListener addListener) {
        this.mContext = mContext;
        this.departmentId = departmentId;
        this.addListener = addListener;
        this.addType = addType;
    }

    public void initView() {
        uids=new UidUtil();
        View dialogView = initDialogView();
        InputUserPhonePresenter presenter = (InputUserPhonePresenter) dialogView.getTag();
        DialogUtils.showCustomizeDialog(mContext, mContext.getText(R.string.input_phone_number).toString(), dialogView, new DialogUtils.DialogListener() {

            @Override
            public boolean onClick(View view) {
                String str = presenter.getSubmitMessage().toString();
                NaizhuAILog.e("str=" + str);
                StringBuilder sbInput=new StringBuilder();
                sbInput.append(inviteUser.getNickName()).append("@").append(inviteUser.getPhoneNumber());
                if (!str.equals(sbInput.toString())) {
                    ToastCompat.showToast(mContext, mContext.getText(R.string.chose_true_number).toString(), Toast.LENGTH_SHORT);
                    return false;
                }
                CompanyManageDataApiStore apiStore = CompanyManageDataApiStoreIml.getInstance();
                uids.add(inviteUser.getUid());
                String companyId = SharedPreferenceUtil.getInstance(mContext).getValue(CompanyBean.COMPANY_ID_KEY, "-1");
                apiStore.insertPersonAddCompanyByIds(uids.toString(), addType, departmentId, powerType,null).subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        addListener.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        addListener.onError(e);
                    }

                    @Override
                    public void onNext(String s) {
                        addListener.onNext(s);
                    }
                });
                return true;
            }
        });
    }

    private View initDialogView() {
        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_person_message, null);
        EditText inputEdit = dialogView.findViewById(R.id.input_phone_number);
        TextView tvSpinnerCompany = dialogView.findViewById(R.id.spinner_chose_company);
        TextView tvSpinnerTitle = dialogView.findViewById(R.id.spinner_chose_title);
        if (addType == ADD_TYPE_TO_COMPANY) {
            tvSpinnerTitle.setText(mContext.getText(R.string.company_power_type));
            String[] addCompanyOperate = mContext.getResources().getStringArray(R.array.company_power_level);
            initListenerTvValue(tvSpinnerCompany, addCompanyOperate);
        } else if (addType == ADD_TYPE_TO_COMPANY_DEPARTMENT) {
            tvSpinnerTitle.setText(mContext.getText(R.string.team_power_type));
            String[] addTeamOperate = mContext.getResources().getStringArray(R.array.user_department_power);
            initListenerTvValue(tvSpinnerCompany, addTeamOperate);
        }
        AutocompletePresenter<PersonUserBean> presenter = setEditAutocomplete(inputEdit);
        dialogView.setTag(presenter);
        return dialogView;
    }

    private AutocompletePresenter<PersonUserBean> setEditAutocomplete(EditText edit) {
        float elevation = 6f;
        Drawable backgroundDrawable = new ColorDrawable(Color.WHITE);
        AutocompletePresenter<PersonUserBean> presenter = new InputUserPhonePresenter(mContext);
        AutocompleteCallback<PersonUserBean> callback = new AutocompleteCallback<PersonUserBean>() {
            @Override
            public boolean onPopupItemClicked(Editable editable, PersonUserBean item) {
                editable.clear();
                editable.append(item.getNickName()).append("@").append(item.getPhoneNumber());
                inviteUser = item;
                return true;
            }

            public void onPopupVisibilityChanged(boolean shown) {
            }
        };

        Autocomplete<PersonUserBean> userAutocomplete;
        userAutocomplete = Autocomplete.<PersonUserBean>on(edit)
                .with(elevation)
                .with(backgroundDrawable)
                .with(presenter)
                .with(callback)
                .build();
        return presenter;
    }


    private void initListenerTvValue(TextView tvSpinner, String[] addTeamOperate) {
        List<String> teamOperate= Arrays.asList(addTeamOperate);
        SpinerPopWindow<String> mSpinerPopWindow = new SpinerPopWindow<String>(mContext,teamOperate);
        PopupItemListener<String> popupItemListener=new PopupItemListener<>(tvSpinner, mSpinerPopWindow, teamOperate);
        popupItemListener.setOnPopUpItemListener(new PopupItemListener.PopupItemOnClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (addType == ADD_TYPE_TO_COMPANY) {
                    switch (position) {
                        case 0:
                            powerType = ConstantsState.COM_MANAGER_POWER_LEVEL;
                            break;
                        case 1:
                            powerType = ConstantsState.COM_NORMAL_POWER_LEVEL;
                            break;
                    }
                } else if (addType == ADD_TYPE_TO_COMPANY_DEPARTMENT) {
                    switch (position) {
                        case 0:
                            powerType = ConstantsState.DEPARTMENT_MANAGER_POWER;
                            break;
                        case 1:
                            powerType = ConstantsState.DEPARTMENT_VICE_MANAGER_POWER;
                            break;
                        case 2:
                            powerType = ConstantsState.DEPARTMENT_NORMAL_POWER;
                            break;
                    }
                }
            }
        });
        mSpinerPopWindow.setOnclickItemListener(popupItemListener);
        mSpinerPopWindow.setOnDismissListener(new PopUpWindowMissListener(tvSpinner, R.mipmap.spinner_drop_arrow_down,mContext));
        tvSpinner.setOnClickListener(new SpinnerListener<>(mSpinerPopWindow, R.mipmap.spinner_drop_arrow_up,mContext));
    }


    public interface AddPersonListener{
        void onCompleted();

        void onError(Throwable e);

        void onNext(String integer);

    }
}
